package com.wwwarehouse.resource_center.adapter.binding_location_code;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.binding_location_code.SelectBindWarehouseBean;

import java.util.List;

import de.greenrobot.event.EventBus;


/**
 * Created by xiaoke.ni on 2017/10/17.
 */

public class BindedLocationCodeAdapter extends RecyclerView.Adapter<BindedLocationCodeAdapter.SelectBindWarehouseHolder> {

    private Activity mActivity;
    /******需要修改的参数*****/
    private List<SelectBindWarehouseBean> mDataList;
    private boolean isLongClicked = false;
    private OnLongClickListener onLongClickListener;
    private OnClickListener onClickListener;
    private PopupWindow popupWindow;

    /******需要修改的参数*****/
    public BindedLocationCodeAdapter(Activity mActivity, List<SelectBindWarehouseBean> dataList, OnLongClickListener onLongClickListener, OnClickListener onClickListener) {
        this.mActivity = mActivity;
        this.mDataList = dataList;
        this.onLongClickListener = onLongClickListener;
        this.onClickListener = onClickListener;
    }

    @Override
    public BindedLocationCodeAdapter.SelectBindWarehouseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_select_bind_warehouse, parent, false);
        return new BindedLocationCodeAdapter.SelectBindWarehouseHolder(view);
    }

    @Override
    public void onBindViewHolder(final BindedLocationCodeAdapter.SelectBindWarehouseHolder holder, int position) {
        /******需要修改的参数*****/
        SelectBindWarehouseBean listBean = mDataList.get(position);
        holder.mBackgroundRl.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                /******需要修改的参数*****/
//                shwoPopupWindow(v, listBean);

//                isLongClicked = !isLongClicked;
//                if (isLongClicked){
//                    holder.mBackgroundRl.setBackgroundColor(mActivity.getResources().getColor(R.color.common_color_c4_121417));
//                    holder.mUnbindingTv.setVisibility(View.VISIBLE);
//                }else{
//                    holder.mBackgroundRl.setBackgroundColor(mActivity.getResources().getColor(R.color.common_color_c3_ffffff));
//                    holder.mUnbindingTv.setVisibility(View.GONE);
//                }
                if (onLongClickListener != null)
                    onLongClickListener.onLongClick(isLongClicked);
                return false;
            }
        });
        holder.mBackgroundRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /******需要修改的参数*****/
//                shwoPopupWindow(holder.mUnbindingTv, listBean);

                if (onClickListener != null)
                    onClickListener.onClick(isLongClicked);
            }
        });
//        holder.mTvAuthName.setText(listBean.getUserName());
//        holder.mTvReason.setText(listBean.getFaileReason());
    }

    @Override
    public int getItemCount() {
        /******需要修改的参数*****/
        return mDataList == null ? 0 : mDataList.size();
    }

    static class SelectBindWarehouseHolder extends RecyclerView.ViewHolder {
        private RelativeLayout mBackgroundRl;
        private TextView mTvName;
        private TextView mUnbindingTv;

        public SelectBindWarehouseHolder(View itemView) {
            super(itemView);
            mBackgroundRl = (RelativeLayout) itemView.findViewById(R.id.background_rl);
            mTvName = (TextView) itemView.findViewById(R.id.tv_name);
            mUnbindingTv = (TextView) itemView.findViewById(R.id.unbinding_tv);
        }
    }

    public interface OnLongClickListener {
        void onLongClick(boolean isLongClicked);
    }

    public interface OnClickListener {
        void onClick(boolean isLongClicked);
    }

    private void shwoPopupWindow(View view, final String itemTempUkid) {
        int[] location = new int[2];
        view.getLocationInWindow(location);
        View popupView = View.inflate(mActivity, R.layout.item_delete_popupwindow_layout, null);
        popupWindow = new PopupWindow(popupView, view.getWidth(), view.getHeight() - 20);
        Drawable drawable = new ColorDrawable(Color.TRANSPARENT);
        LinearLayout mDeleteLayout = (LinearLayout) popupView.findViewById(R.id.delete_layout);
        mDeleteLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                /******需要修改的参数:调删除接口*****/
//                EventBus.getDefault().post(new DeleteEvent(itemTempUkid));

            }
        });
        popupWindow.setBackgroundDrawable(drawable);
        popupWindow.setOutsideTouchable(true);
        int x = location[0];
        int y = location[1];
        popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, x, y);
    }

    public void dissPop() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }
}
